package com.example.child_protection.service.impl;

import com.example.child_protection.dao.RoleDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.RoleInsertRequestDTO;
import com.example.child_protection.dto.request.update.RoleUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.RoleModel;
import com.example.child_protection.model.SystemRegionModel;
import com.example.child_protection.service.RoleManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 角色信息管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class RoleManageServiceImpl extends MyBaseService implements RoleManageService {

    @Autowired
    public RoleDao roleDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(RoleInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_INSERT, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<RoleInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(RoleInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                RoleModel model = new RoleModel();
                BeanUtils.copyProperties(requestDTO,model);
                roleDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(RoleUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_UPDATE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<RoleUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(RoleUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                RoleModel model = new RoleModel();
                BeanUtils.copyProperties(requestDTO,model);
                roleDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                RoleModel model = new RoleModel();
                BeanUtils.copyProperties(requestDTO,model);
                roleDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

}
